package com.hdd.hll.modules.info.service.impl;


import com.hdd.hll.modules.info.bean.Coal;
import com.hdd.hll.modules.info.bean.CoalCompanys;
import com.hdd.hll.modules.info.bean.Company;
import com.hdd.hll.modules.info.mapper.CoalMapper;
import com.hdd.hll.modules.info.mapper.CompanyMapper;
import com.hdd.hll.modules.info.service.InformationService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class InformationServiceImpl implements InformationService {

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    CoalMapper coalMapper;

    @Autowired
    RedissonClient redissonClient;

    @Override
    public CoalCompanys findAll() throws InterruptedException {
        RLock lock = redissonClient.getLock("key"+1);
        boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        if (res) {
            try {
                CoalCompanys coalCompanys = new CoalCompanys();
                //收发运类型(1,收货方,2发货方,4运输方)
                List<Integer> collector = new ArrayList<>();
                collector.add(1);
                collector.add(3);
                collector.add(7);
                // 收
                List<Company> collectors = companyMapper.findList(collector);
                coalCompanys.setProvides(collectors);

                //收发运类型(1,收货方,2发货方,4运输方)
                List<Integer> provide = new ArrayList<>();
                provide.add(2);
                provide.add(3);
                provide.add(7);
                // 发
                List<Company> provides = companyMapper.findList(provide);
                coalCompanys.setCollectors(provides);
                // 煤
                List<Coal> coals = coalMapper.findList();
                coalCompanys.setCoals(coals);
                return coalCompanys;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }

    @Override
    public List<Company> findCollects() throws InterruptedException {
        RLock lock = redissonClient.getLock("key"+2);
        boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        if (res) {
            try {
                //收发运类型(1,收货方,2发货方,4运输方)
                List<Integer> provide = new ArrayList<>();
                provide.add(2);
                provide.add(3);
                provide.add(7);
                // 发
                List<Company> provides = companyMapper.findList(provide);
                return provides;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }

    @Override
    public List<Company> findProvides() throws InterruptedException {
        RLock lock = redissonClient.getLock("key"+3);
        boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        if (res) {
            try {
                //收发运类型(1,收货方,2发货方,4运输方)
                List<Integer> collector = new ArrayList<>();
                collector.add(1);
                collector.add(3);
                collector.add(7);
                // 收
                List<Company> collectors = companyMapper.findList(collector);
                return collectors;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }

    @Override
    public List<Coal> findCoals() throws InterruptedException {
        RLock lock = redissonClient.getLock("key"+4);
        boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
        if (res) {
            try {
                // 煤
                List<Coal> coals = coalMapper.findList();
                return coals;
            } finally {
                lock.unlock();
            }
        }
        return null;
    }
}
